//
//  MainViewController.m
//  BSLCar
//
//  Created by 梁毅 on 15/5/27.
//  Copyright (c) 2015年 lyz. All rights reserved.
//

#import "MainViewController.h"

#import "VAGuideView.h"
#import "Common.h"
#import "UIImage+Ex.h"

@interface MainViewController ()

{
    VAGuideView *_guideView;
    UIInterfaceOrientation _lockRotate;
    UIInterfaceOrientation _currInterfaceOrientation;
}

@end

@implementation MainViewController
- (void)dealloc
{
    [_menuTableview release];
    [_backView release];
    [_beginLabel release];
    [_backView release];
    [_airMenuController release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];

    UIImageView *imagev = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"iPhone_guide_bg"]];

    UIBarButtonItem *item = [[UIBarButtonItem alloc] init];
    item.title = @"返回";
    self.navigationItem.backBarButtonItem = item;
    [item release];
    imagev.frame = self.view.frame;
    [self.view addSubview:imagev];
    [self showGuide];
    [imagev release];
    
}

-(void)showGuide {
    if ([VAGuideView shouldShowGuide]) {
        NSMutableArray *arrImage = [NSMutableArray array];
        //#warning  这里填你们到导航图  for循环控制张数   后面是图片的名字
        for (int i=0; i<4; i++) {
            UIImage *image = [UIImage imageWithFileName:[NSString stringWithFormat:@"%d", i+1] ofType:@"png"];
            [arrImage addObject:image];
        }
        _guideView = [VAGuideView guideViewWithFrame:self.view.bounds arrImages:arrImage space:30];
        //引导页背景图
        UIImageView *imageViewBg = [[UIImageView alloc] initWithFrame:_guideView.bounds];
        //#warning 这里是播放导航图时的背景图片  换成你们自己的即可
        imageViewBg.image = [UIImage imageNamed:@"iPhone_guide_bg"];
        imageViewBg.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
        imageViewBg.contentMode = UIViewContentModeScaleAspectFill;
        imageViewBg.clipsToBounds = YES;
        [_guideView insertSubview:imageViewBg atIndex:0];
        _guideView.imageContentMode = UIViewContentModeCenter;
        
        //最后一页进入应用按钮
        UIButton *btnStart = [UIButton buttonWithType:UIButtonTypeCustom];
        [btnStart setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        //#warning  这是最后一页导航图上面按钮的控制，图片，文字，位置 点击方法（点击之后才创建首页视图,因为导航图消失时有动画效果，建议加个定时器，1秒之后才开始创建视图）
        [btnStart setBackgroundImage:[UIImage imageNamed:@"iPhone-anniubox-2"] forState:UIControlStateNormal];
        [btnStart setTitle:@"点击进入" forState:UIControlStateNormal];
        btnStart.frame = CGRectMake(self.view.bounds.size.width / 2 - 80, self.view.bounds.size.height - 100, 160, 40);
        [btnStart addTarget:self action:@selector(touchStartButton) forControlEvents:UIControlEventTouchUpInside];
        btnStart.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleTopMargin;
        [_guideView addButtonAtLastPage:btnStart];
        if (_guideView && arrImage.count>0) {
            _currInterfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
            _lockRotate = YES;
            [self.view addSubview:_guideView];
            double delayInSeconds = 0;
            dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
            dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
                [_guideView startAnimationWithDuration:3
                                 animationComplecation:nil
                                            didDismiss:^{
                                                _lockRotate = NO;
                                            }];
            });
        }
        [imageViewBg release];
    }

    //#warning 不是第一次启动，直接创建本页面视图
    else
    {
        _currInterfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
        _lockRotate = YES;
        [self createWelcomeView];
    }
}

-(void)touchStartButton
{
    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(createWelcomeView) userInfo:nil repeats:NO];
}

- (void)createWelcomeView
{

    _menuTableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 83 *FitHeight, 210 *FitWidth, 505 *FitHeight  + 64)];
    _menuTableview.delegate = self;
    _menuTableview.dataSource = self;
    _menuTableview.backgroundColor = [UIColor clearColor];
    _menuTableview.separatorStyle = NO;
    [self.view addSubview:_menuTableview];
    //初始化菜单栏试图控制器
    self.airMenuController = [XDKAirMenuController sharedMenu];

    self.airMenuController.airDelegate = self;

    self.airMenuController.isMenuOnRight = false;

    [self.view addSubview:self.airMenuController.view];
    [self addChildViewController:self.airMenuController];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        MenuCell *cell = [[MenuCell alloc] init];
        cell.backgroundColor = [UIColor clearColor];
//        cell.leftView.image = [UIImage imageNamed:[NSString stringWithFormat:@"iconfont-shouye"]];
        cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"iconfont-shouye"]];
        cell.imageView.highlightedImage = [UIImage imageNamed:@"iconfont-shouye-2"];
        
        cell.myTitleLabel.text = @"首页";
        cell.myTitleLabel.textColor = [UIColor blackColor];
        return cell;
    }

    if (indexPath.row == 1) {
        MenuCell *cell = [[MenuCell alloc] init];
        cell.backgroundColor = [UIColor clearColor];
        
        cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"iconfont-mags"]];
        cell.imageView.highlightedImage = [UIImage imageNamed:@"iconfont-mags-3"];
        cell.myTitleLabel.text = @"杂志";
        cell.myTitleLabel.textColor = [UIColor blackColor];
        return cell;
    }
    if (indexPath.row == 2) {
        MenuCell *cell = [[MenuCell alloc] init];
        cell.backgroundColor = [UIColor clearColor];

        cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"iconfont-xinwen2"]];
        cell.imageView.highlightedImage = [UIImage imageNamed:@"iconfont-xinwen2-1"];

        cell.myTitleLabel.text = @"新闻";
        cell.myTitleLabel.textColor = [UIColor blackColor];
        return cell;
    }
    if (indexPath.row == 3) {
        MenuCell *cell = [[MenuCell alloc] init];
        cell.backgroundColor = [UIColor clearColor];

        cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"iconfont-ordinaryset"]];
        cell.imageView.highlightedImage = [UIImage imageNamed:@"iconfont-ordinaryset-2"];

        cell.myTitleLabel.text = @"设置";
        cell.myTitleLabel.textColor = [UIColor blackColor];
        return cell;
    }
    UITableViewCell *cell = [[UITableViewCell alloc] init];
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80 *FitHeight;
}

- (UIViewController *)airMenu:(XDKAirMenuController *)airMenu viewControllerAtIndexPath:(NSIndexPath *)indexPath
{
    UIViewController *vc = nil;
    if (indexPath.row == 0)
    {
        
        FirstPageViewController *uv = [[FirstPageViewController alloc]init];
        UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:uv];

        vc = nav;
        [uv release];
    }
   
    if (indexPath.row == 1)
    {

        JournalViewController *uv = [[JournalViewController alloc]init];
        UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:uv];

        vc = nav;
        [uv release];

    }
    if (indexPath.row == 2)
    {

        NewsViewController *uv = [[NewsViewController alloc]init];
        UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:uv];

        vc = nav;
        [uv release];

    }
    if (indexPath.row == 3)
    {
        MyViewController *uv = [[MyViewController alloc]init];
        UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:uv];

        vc = nav;
        [uv release];
    }
    return vc;
}
- (UITableView*)tableViewForAirMenu:(XDKAirMenuController*)airMenu
{
    return _menuTableview;
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
